﻿using System;
using System.Data;
using System.Globalization;
using System.IO;
using wojilu.Apps.Qycms.Domain;
using wojilu.Apps.Qycms.Helper;
using wojilu.IO;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class PublicController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }

        public PublicController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet]
        public virtual void FileWindow()
        {
            base.CheckPermission();
            SetPublic();
            setpublic();
            set("load", to(new AdminController().Load) + "?");
            set("iframename", ctx.Get("iframename"));
            var checkfrom = ctx.Get("checkfrom");
            set("checkfrom", checkfrom);
            set("getbyid", ctx.Get("getbyid"));
            set("iframeid", ctx.Get("iframeid"));
            var fileinputid = ctx.Get("fileinputid");
            set("fileinputid", fileinputid);
            set("digheight", ctx.Get("digheight"));
            set("rootDIR", strUtil.Join(sys.Path.Root, ctx.Get("rootDIR")));
            var dirlist = ctx.Get("dirlist");
            var filetype = ctx.Get("filetype");
            set("filetype", filetype);
            var maxselect = ctx.Get("maxselect");
            set("picview", to(AlbumList) + "?dirlist=" + dirlist + "&checkfrom=" + checkfrom + "&fileinputid=" + fileinputid + "&filetype=" + filetype + "&maxselect=" + maxselect);
            set("framt", ctx.Get("digheight"));
            set("fileview", to(FileList) + "?dirlist=" + dirlist + "&checkfrom=" + checkfrom + "&fileinputid=" + fileinputid + "&filetype=" + filetype + "&maxselect=" + maxselect);
            set("loadurl", to(UpFile) + "?dirlist=" + dirlist + "&checkfrom=" + checkfrom + "&fileinputid=" + fileinputid + "&filetype=" + filetype + "&maxselect=" + maxselect);
            set("batupwindow", to(BatupFile) + "?dirlist=" + dirlist + "&checkfrom=" + checkfrom + "&fileinputid=" + fileinputid + "&filetype=" + filetype + "&maxselect=" + maxselect);


        }

        [HttpGet]
        public virtual void AlbumList()
        {
            base.CheckPermission();
            SetPublic();
            setpublic();
            set("load", to(new AdminController().Load) + "?");
            set("iframename", ctx.Get("iframename"));
            set("digheight", ctx.Get("digheight"));
            set("fheight", ctx.GetInt("fheight") - 45);
            set("loadurl", to(PicList) + "?checkfrom=" + ctx.Get("checkfrom"));
            var maxselect = ctx.GetInt("maxselect");
            set("maxselect", maxselect);
            var list = Album_Images.find(" isclass=1 order by id desc").list();
            bind("list", list);
            set("albumarray", to(Albumarray));
            var l = Lng.find("isuptype=:i order by id asc").set("i", 0).list();
            bind("listlng", l);
            var lng = DefaultLng;
            set("lng", lng);
        }

        [HttpGet]
        public virtual void PicList()
        {
            base.CheckPermission();
            var amid = ctx.Get("amid");
            var list = Album_File.find("amid=:a").set("a", amid).list();
            var builder = new MyStringBuilder();
            Configs c = Configs.find("valname=:v").set("v", "domain").first();
            if (list.Count > 0)
            {
                builder.AppendLine("<ul>");
                foreach (var row in list)
                {
                    var fileallpath = String.Empty;
                    var checkfrom = ctx.Get("checkfrom");
                    if (checkfrom == "picshow")
                    {
                        fileallpath = sys.Path.Root + row.filepath + row.filename;
                    }
                    else if (checkfrom == "function")
                    {
                        fileallpath = sys.Path.Root + row.filepath + row.filename;
                    }
                    else
                    {
                        fileallpath = row.filepath + row.filename;
                    }
                    if (row.iswidth == 1)
                    {
                        builder.AppendLine("		<li id=\"albumpic" + row.Id + "\" onclick=\"alselected('albumpic" + row.Id + "','" + "" + fileallpath + "','selected'," + row.iswidth + ");\"><p><img src=\"" + strUtil.Join(c.value, fileallpath) + "\" width=\"100\"><span class=\"panel_checkbox\">已选中</span></p></li>");
                    }
                    else
                    {
                        builder.AppendLine("		<li id=\"albumpic" + row.Id + "\" onclick=\"alselected('albumpic" + row.Id + "','" + "" + fileallpath + "','selected'," + row.iswidth + ");\"><p><img src=\"" + strUtil.Join(c.value, fileallpath) + fileallpath + "\" height=\"100\"><span class=\"panel_checkbox\">已选中</span></p></li>");
                    }
                }
                builder.AppendLine("</ul>");
            }
            else
            {
                builder.AppendLine(lang("filemanage_filecheck_select_fileno"));
            }
            ctx.web.ResponseWrite(builder.ToString());
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void Albumarray()
        {
            base.CheckPermission();
            var lng = ctx.Get("lng");
            var list = Album_Images.find("lng=:l").set("l", lng).list();
            var builder = new MyStringBuilder();
            builder.Append("<select size=\"1\" name=\"amid\" id=\"amid\"  onchange=\"picload(this.value)\">");
            builder.Append(" <option value=\"0\">请选择相册</option>");
            foreach (var row in list)
            {
                builder.Append("<option value=\"" + row.Id + "\">" + row.title + "</option>");
            }
            builder.Append("</select>");
            ctx.web.ResponseWrite(builder.ToString());
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void FileList()
        {
            base.CheckPermission();
            set("fheight", ctx.GetInt("fheight") - 45);
            set("loadurl", String.Empty);
            var maxselect = ctx.GetInt("maxselect");
            set("maxselect", maxselect);
            Configs c = Configs.find("valname=:v").set("v", "upfile_dir").first();
            set("filepath", c.value);
            var updirtype = 0;
            var dirlist = ctx.Get("dirlist");
            if (!dirlist.StrIsNullOrEmpty())
            {
                updirtype = 1;
            }
            else
            {
                updirtype = 2;
            }
            set("dirlist", dirlist);
            set("updirtype", updirtype);
            var checkfrom = ctx.Get("checkfrom");
            var picurl = String.Empty;
            if (checkfrom == "picshow")
            {
                picurl = sys.Path.Root + c.value + dirlist;
            }
            else if (checkfrom == "editwin")
            {
                picurl = c.value + dirlist;
            }
            else if (checkfrom == "function")
            {
                picurl = sys.Path.Root + c.value + dirlist;
            }
            else if (checkfrom == "edit")
            {
                picurl = c.value + dirlist;
            }
            else
            {
                picurl = sys.Path.Root + c.value + dirlist;
            }
            set("picurl", picurl);
            var fileinputid = ctx.Get("fileinputid");
            var filetype = ctx.Get("filetype");
            var fheight = ctx.GetInt("fheight");
            var uploadpath = "/" + Left(dirlist, dirlist.Length - 1);
            uploadpath = Left(uploadpath, uploadpath.LastIndexOf('/')).TrimStart('/');
            if (!uploadpath.StrIsNullOrEmpty()) uploadpath += "/";
            set("uploadurl", to(FileList) + "?checkfrom=" + checkfrom + "&fileinputid=" + fileinputid + "&filetype=" + filetype + "&maxselect=" + maxselect + "&fheight=" + fheight + "&dirlist=" + uploadpath);
            var dt = FileSystemObject.GetDirectoryInfos(sys.Path.Root + c.value + dirlist, FsoMethod.All);
            dt.Columns.Add("iswidth", typeof(Int32));
            dt.Columns.Add("fid", typeof(String));
            dt.Columns.Add("loadurl", typeof(String));
            dt.Columns.Add("loadurl2", typeof(String));
            var c1 = Configs.find("valname=:v").set("v", "domain").first();
            set("adminurl", c1.value);
            foreach (DataRow row in dt.Rows)
            {
                var url = strUtil.Join(strUtil.Join(strUtil.Join(c1.value, c.value), dirlist), row["name"].ToString());
                var fid = DESEncrypt.Encrypt(url, 1);
                row["fid"] = fid;
                row["iswidth"] = 1;
                row["loadurl"] = to(FileList) + "?checkfrom=" + checkfrom + "&fileinputid=" + fileinputid + "&filetype=" + filetype + "&maxselect=" + maxselect + "&fheight=" + fheight + "&dirlist=" + dirlist + row["name"] + "/";
                row["loadurl2"] = String.Empty;
            }
            bind("dt", dt);
        }

        [HttpGet]
        public virtual void UpFile()
        {
            base.CheckPermission();
            SetPublic();
            setpublic();
            set("load", to(new AdminController().Load) + "?");
            set("fheight", ctx.GetInt("fheight") - 45);
            target(UpfileSave);
            set("path", configsService.GetValue("upfile_dir"));
            var checkfrom = ctx.Get("checkfrom");
            set("checkfrom", checkfrom);
            set("maxfile", configsService.GetValue("upfile_maxsize"));
            set("img_width", configsService.GetValue("img_width"));
            set("img_height", configsService.GetValue("img_height"));
            var filetype = ctx.Get("filetype");
            set("filetype", filetype);
            var lng = DefaultLng;
            set("lng", lng);
            set("img_iswater", configsService.GetValue("img_iswater"));
            set("img_issmallpic", configsService.GetValue("img_issmallpic"));
            var list = Album_Images.find(" isclass=1 order by id desc").list();
            bind("list", list);
            var upfile_maxsize = configsService.GetValue("upfile_maxsize");
            var strArray = strUtil.Split(configsService.GetBottonName("upfile_maxsize"), ",");
            var maxfile2 = string.Empty;
            for (var i = 0; i < strArray.Length; i++)
            {
                var strArray1 = strUtil.Split(strArray[i], "|");
                if (strArray1[0] == upfile_maxsize)
                {
                    maxfile2 = strArray1[1];
                }
            }
            set("maxfile2", maxfile2);
            var upfile_pictype = String.Empty;
            switch (ctx.Get("filetype"))
            {
                case "img":
                    upfile_pictype = configsService.GetValue("upfile_pictype").GetReplace("|", "、");
                    break;

                case "media":
                    upfile_pictype = configsService.GetValue("uifile_movertype").GetReplace("|", "、");
                    break;

                default:
                    upfile_pictype = configsService.GetValue("upfile_filetype").GetReplace("|", "、");
                    break;
            }
            set("upfile_pictype", upfile_pictype);
        }

        [HttpPost, HttpGet]
        public virtual void UpfileSave()
        {
            base.CheckPermission();
            var checkfrom = ctx.Get("checkfrom");
            var img_width = ctx.PostInt("img_width");
            var img_height = ctx.PostInt("img_height");
            var isamid = ctx.PostInt("isamid");
            var amid = ctx.PostLong("amid");
            var title = ctx.Post("title");
            var img_iswater = ctx.PostInt("img_iswater");
            var img_issmallpic = ctx.PostInt("img_issmallpic");
            double filesize = 0.00;
            var filetype = ctx.Post("filetype");
            var path = ctx.Post("path");
            var lngs = ctx.Post("lng");
            switch (filetype)
            {
                case "img":
                    #region img
                    var filename = Path.GetFileName(ctx.GetFileSingle().FileName);
                    var fileextname = Path.GetExtension(filename).ToLower();
                    var filetype1 = ctx.GetFileSingle().ContentType.ToLower();
                    if (!StringArray.InArray(fileextname.TrimStart('.'), configsService.GetValue("upfile_pictype"), "|") && !filetype1.StartsWith("image"))
                    {
                        ctx.web.ResponseWrite("您上传的文件格式不受系统支持！");
                        ctx.web.ResponseEnd();
                    }
                    filesize = ctx.GetFileSingle().ContentLength;
                    if (filesize > cvt.ToInt(configsService.GetValue("upfile_maxsize")))
                    {
                        ctx.web.ResponseWrite("文件大小超过限制！");
                        ctx.web.ResponseEnd();
                    }
                    var FilePath = path + DateTime.Now.ToString("yyyy/MM/dd/", DateTimeFormatInfo.InvariantInfo);
                    var FileName = (DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.CurrentCulture) + "_" + Rand.MakeRandomString("0123456789", 3));
                    if (!FileSystemObject.IsExist(PathHelper.Map(strUtil.Join(sys.Path.Root, FilePath)), FsoMethod.Folder))
                    {
                        FileSystemObject.Create(PathHelper.Map(strUtil.Join(sys.Path.Root, FilePath)), FsoMethod.Folder);
                    }
                    var OFilePath = strUtil.Join(sys.Path.Root, FilePath + FileName + fileextname);
                    ctx.GetFileSingle().SaveAs(PathHelper.Map(OFilePath));
                    if (img_issmallpic == 1)
                    {
                        var s_FilePath = strUtil.Join(sys.Path.Root, FilePath + FileName + "_small" + fileextname);
                        Thumbnail.MakeThumbnailImage(PathHelper.Map(OFilePath), PathHelper.Map(s_FilePath), img_width, img_height);
                    }
                    if (img_iswater > 0)
                    {
                        var w_FilePath = strUtil.Join(sys.Path.Root, FilePath + FileName + "_W" + fileextname);
                        if (img_iswater == 1)
                        {
                            Thumbnail.TextWatermark(PathHelper.Map(OFilePath), configsService.GetValue("img_wmt_text"), "Verdana", cvt.ToInt(configsService.GetValue("img_wmt_size")), configsService.GetValue("img_wmt_color"), cvt.ToInt(configsService.GetValue("img_wmt_transparent")), cvt.ToInt(configsService.GetValue("img_wmt_pos")), PathHelper.Map(w_FilePath));
                            FileSystemObject.Delete(PathHelper.Map(OFilePath), FsoMethod.File);
                            FileSystemObject.Move(PathHelper.Map(w_FilePath), PathHelper.Map(OFilePath), FsoMethod.File);
                        }
                        else if (img_iswater == 2)
                        {
                            Thumbnail.ImageWatermark(PathHelper.Map(OFilePath), PathHelper.Map(strUtil.Join(sys.Path.Root, "Images/" + configsService.GetValue("img_wmi_file"))), cvt.ToInt(configsService.GetValue("img_wmi_transparent")), cvt.ToInt(configsService.GetValue("img_wmi_pos")), PathHelper.Map(w_FilePath));
                            FileSystemObject.Delete(PathHelper.Map(OFilePath), FsoMethod.File);
                            FileSystemObject.Move(PathHelper.Map(w_FilePath), PathHelper.Map(OFilePath), FsoMethod.File);
                        }
                    }
                    var iswidth = 1;
                    FileName modelFileName = new FileName();
                    modelFileName.username = ((User)ctx.viewer.obj).Name;
                    modelFileName.filename = FileName + fileextname;
                    modelFileName.filetype = filetype;
                    modelFileName.filepath = FilePath;
                    modelFileName.addtime = DateTime.Now;
                    modelFileName.iswidth = iswidth;
                    Result result = modelFileName.insert();
                    if (isamid == 0)
                    {
                        if (amid != 0)
                        {
                            Album_File modelAlbum_File = new Album_File();
                            modelAlbum_File.amid = amid;
                            modelAlbum_File.fiid = cvt.ToInt(db.RunScalar("select max(fiid) from ybb_album_file")) + 1;
                            modelAlbum_File.filetitle = FileName + fileextname;
                            modelAlbum_File.filename = FileName + fileextname;
                            modelAlbum_File.filepath = FilePath;
                            modelAlbum_File.iswidth = iswidth;
                            modelAlbum_File.addtime = DateTime.Now;
                            modelAlbum_File.insert();
                        }
                    }
                    else
                    {
                        Album_Images modelAlbumImages = new Album_Images();
                        modelAlbumImages.pid = 50;
                        modelAlbumImages.lng = lngs;
                        modelAlbumImages.title = title;
                        modelAlbumImages.addtime = DateTime.Now;
                        modelAlbumImages.isclass = 1;
                        modelAlbumImages.istop = 0;
                        modelAlbumImages.insert();
                        amid = modelAlbumImages.Id;

                        Album_File modelAlbum_File = new Album_File();
                        modelAlbum_File.amid = amid;
                        modelAlbum_File.fiid = cvt.ToInt(db.RunScalar("select max(fiid) from ybb_album_file")) + 1;
                        modelAlbum_File.filetitle = FileName + fileextname;
                        modelAlbum_File.filename = FileName + fileextname;
                        modelAlbum_File.filepath = FilePath;
                        modelAlbum_File.iswidth = iswidth;
                        modelAlbum_File.addtime = DateTime.Now;
                        modelAlbum_File.insert();
                    }
                    ctx.web.ResponseWrite(strUtil.Join(sys.Path.Root, FilePath + FileName + fileextname + "|" + filetype + "|" + iswidth));
                    ctx.web.ResponseEnd();
                    #endregion
                    break;

                case "media":
                case "mover":
                    #region media and mover
                    filename = System.IO.Path.GetFileName(ctx.GetFileSingle().FileName);
                    fileextname = System.IO.Path.GetExtension(filename).ToLower();
                    if (!StringArray.InArray(fileextname.TrimStart('.'), configsService.GetValue("uifile_movertype"), "|"))
                    {
                        ctx.web.ResponseWrite("您上传的文件格式不受系统支持！");
                        ctx.web.ResponseEnd();
                    }
                    filesize = ctx.GetFileSingle().ContentLength;
                    if (filesize > cvt.ToInt(configsService.GetValue("upfile_maxsize")))
                    {
                        ctx.web.ResponseWrite("文件大小超过限制！");
                        ctx.web.ResponseEnd();
                    }
                    FilePath = path + DateTime.Now.ToString("yyyy/MM/dd/", DateTimeFormatInfo.InvariantInfo);
                    FileName = (DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.CurrentCulture) + "_" + Rand.MakeRandomString("0123456789", 3));
                    if (!FileSystemObject.IsExist(PathHelper.Map(strUtil.Join(sys.Path.Root, FilePath)), FsoMethod.Folder))
                    {
                        FileSystemObject.Create(PathHelper.Map(strUtil.Join(sys.Path.Root, FilePath)), FsoMethod.Folder);
                    }
                    OFilePath = strUtil.Join(sys.Path.Root, FilePath + FileName + fileextname);
                    ctx.GetFileSingle().SaveAs(PathHelper.Map(OFilePath));
                    if (img_iswater > 0)
                    {
                        var w_FilePath = strUtil.Join(sys.Path.Root, FilePath + FileName + "_W" + fileextname);
                        if (img_iswater == 1)
                        {
                            Thumbnail.TextWatermark(PathHelper.Map(OFilePath), configsService.GetValue("img_wmt_text"), "Verdana", cvt.ToInt(configsService.GetValue("img_wmt_size")), configsService.GetValue("img_wmt_color"), cvt.ToInt(configsService.GetValue("img_wmt_transparent")), cvt.ToInt(configsService.GetValue("img_wmt_pos")), PathHelper.Map(w_FilePath));
                            FileSystemObject.Delete(PathHelper.Map(OFilePath), FsoMethod.File);
                            FileSystemObject.Move(PathHelper.Map(w_FilePath), PathHelper.Map(OFilePath), FsoMethod.File);
                        }
                        else if (img_iswater == 2)
                        {
                            Thumbnail.ImageWatermark(PathHelper.Map(OFilePath), PathHelper.Map(strUtil.Join(sys.Path.Root, "Images/" + configsService.GetValue("img_wmi_file"))), cvt.ToInt(configsService.GetValue("img_wmi_transparent")), cvt.ToInt(configsService.GetValue("img_wmi_pos")), PathHelper.Map(w_FilePath));
                            FileSystemObject.Delete(PathHelper.Map(OFilePath), FsoMethod.File);
                            FileSystemObject.Move(PathHelper.Map(w_FilePath), PathHelper.Map(OFilePath), FsoMethod.File);
                        }
                    }
                    iswidth = 3;
                    modelFileName = new FileName();
                    modelFileName.username = ((User)ctx.viewer.obj).Name;
                    modelFileName.filename = FileName + fileextname;
                    modelFileName.filetype = "file";
                    modelFileName.filepath = FilePath;
                    modelFileName.addtime = DateTime.Now;
                    modelFileName.iswidth = iswidth;
                    modelFileName.insert();
                    ctx.web.ResponseWrite(strUtil.Join(sys.Path.Root, FilePath + FileName + fileextname + "|file|" + iswidth));
                    ctx.web.ResponseEnd();
                    #endregion
                    break;

                case "file":
                    #region file
                    filename = System.IO.Path.GetFileName(ctx.GetFileSingle().FileName);
                    fileextname = System.IO.Path.GetExtension(filename).ToLower();
                    if (!StringArray.InArray(fileextname.TrimStart('.'), configsService.GetValue("upfile_filetype"), "|"))
                    {
                        ctx.web.ResponseWrite("您上传的文件格式不受系统支持！");
                        ctx.web.ResponseEnd();
                    }
                    filesize = ctx.GetFileSingle().ContentLength;
                    if (filesize > cvt.ToInt(configsService.GetValue("upfile_maxsize")))
                    {
                        ctx.web.ResponseWrite("文件大小超过限制！");
                        ctx.web.ResponseEnd();
                    }
                    FilePath = path + DateTime.Now.ToString("yyyy/MM/dd/", DateTimeFormatInfo.InvariantInfo);
                    FileName = (DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.CurrentCulture) + "_" + Rand.MakeRandomString("0123456789", 3));
                    if (!FileSystemObject.IsExist(PathHelper.Map(strUtil.Join(sys.Path.Root, FilePath)), FsoMethod.Folder))
                    {
                        FileSystemObject.Create(PathHelper.Map(strUtil.Join(sys.Path.Root, FilePath)), FsoMethod.Folder);
                    }
                    OFilePath = strUtil.Join(sys.Path.Root, FilePath + FileName + fileextname);
                    ctx.GetFileSingle().SaveAs(PathHelper.Map(OFilePath));
                    iswidth = 3;
                    modelFileName = new FileName();
                    modelFileName.username = ((User)ctx.viewer.obj).Name;
                    modelFileName.filename = FileName + fileextname;
                    modelFileName.filetype = "file";
                    modelFileName.filepath = FilePath;
                    modelFileName.addtime = DateTime.Now;
                    modelFileName.iswidth = iswidth;
                    modelFileName.insert();
                    ctx.web.ResponseWrite(strUtil.Join(sys.Path.Root, FilePath + FileName + fileextname + "|file|" + iswidth));
                    ctx.web.ResponseEnd();
                    #endregion
                    break;
            }

        }

        [HttpGet]
        public virtual void BatupFile()
        {
            base.CheckPermission();
            SetPublic();
            setpublic();
            set("load", to(new AdminController().Load) + "?");
            set("fheight", ctx.GetInt("fheight") - 45);
            set("batupfilesave", to(BatupFileSave));
            set("path", ctx.Get("dirlist"));
            set("filetype", ctx.Get("filetype"));
            set("maxfile", configsService.GetValue("upfile_maxsize"));
            var upfile_pictype = configsService.GetValue("upfile_pictype");
            var str = String.Empty;
            foreach (var row in upfile_pictype.SplitString("|"))
            {
                str += "*." + row + ";";
            }
            str = str.Remove(str.Length - 1);
            set("upfile_pictype", str);
            var maxselect = ctx.GetInt("maxselect");
            set("maxselect", maxselect);
            var upfile_maxsize = configsService.GetValue("upfile_maxsize");
            var strArray = strUtil.Split(configsService.GetBottonName("upfile_maxsize"), ",");
            var maxfile2 = string.Empty;
            for (var i = 0; i < strArray.Length; i++)
            {
                var strArray1 = strUtil.Split(strArray[i], "|");
                if (strArray1[0] == upfile_maxsize)
                {
                    maxfile2 = strArray1[1];
                }
            }
            set("maxfile2", maxfile2);
            var upfile_pictype_str = String.Empty;
            switch (ctx.Get("filetype"))
            {
                case "img":
                    upfile_pictype_str = upfile_pictype.GetReplace("|", "、");
                    break;

                case "media":
                    upfile_pictype_str = upfile_pictype.GetReplace("|", "、");
                    break;

                default:
                    upfile_pictype_str = upfile_pictype.GetReplace("|", "、");
                    break;
            }
            set("upfile_pictype_str", upfile_pictype_str);
            set("img_iswater", configsService.GetValue("img_iswater"));
            var ybb_admininfo = (User)ctx.viewer.obj;
            set("ybb_admininfo", DES.Encrypt(ybb_admininfo.Name, ""));
            set("ybb_powerlist", DES.Encrypt(ybb_admininfo.GroupId.ToString(), ""));
            var list = Album_Images.find(" isclass=1 order by id desc").list();
            bind("list", list);
        }

        [HttpGet, HttpPost]
        public virtual void BatupFileSave()
        {
            var ybb_admininfo = ctx.Get("ybb_admininfo");
            var ybb_powerlist = ctx.Get("ybb_powerlist");
            if (ybb_admininfo == null || ybb_powerlist == null)
            {
                echoRedirect(lang("exPlsLogin"), t2(new AdminController().Login) + "?returnUrl=" + ctx.url.EncodeUrl);
                return;
            }
            var img_iswater = ctx.PostInt("img_iswater");
            double filesize = 0.00;
            var amid = ctx.PostLong("amid");
            var filetype = ctx.Get("filetype");
            var path = ctx.Post("path");
            var lngs = ctx.Post("lng");
            var img_width = ctx.PostInt("img_width");
            var img_height = ctx.PostInt("img_height");
            var img_issmallpic = ctx.PostInt("img_issmallpic");
            switch (filetype)
            {
                case "img":
                    var filename = Path.GetFileName(ctx.GetFileSingle().FileName);
                    var fileextname = Path.GetExtension(filename).ToLower();
                    var filetype1 = ctx.GetFileSingle().ContentType.ToLower();
                    if (!StringArray.InArray(fileextname.TrimStart('.'), configsService.GetValue("upfile_pictype"), "|") && !filetype1.StartsWith("image"))
                    {
                        ctx.web.ResponseWrite("您上传的文件格式不受系统支持！");
                        ctx.web.ResponseEnd();
                    }
                    filesize = ctx.GetFileSingle().ContentLength;
                    if (filesize > cvt.ToInt(configsService.GetValue("upfile_maxsize")))
                    {
                        ctx.web.ResponseWrite("文件大小超过限制！");
                        ctx.web.ResponseEnd();
                    }
                    var FilePath = path + DateTime.Now.ToString("yyyy/MM/dd/", DateTimeFormatInfo.InvariantInfo);
                    var FileName = (DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.CurrentCulture) + "_" + Rand.MakeRandomString("0123456789", 3));
                    if (!FileSystemObject.IsExist(PathHelper.Map(strUtil.Join(sys.Path.Root, FilePath)), FsoMethod.Folder))
                    {
                        FileSystemObject.Create(PathHelper.Map(strUtil.Join(sys.Path.Root, FilePath)), FsoMethod.Folder);
                    }
                    var OFilePath = strUtil.Join(sys.Path.Root, FilePath + FileName + fileextname);
                    ctx.GetFileSingle().SaveAs(PathHelper.Map(OFilePath));
                    if (img_issmallpic == 1)
                    {
                        var s_FilePath = strUtil.Join(sys.Path.Root, FilePath + FileName + "_small" + fileextname);
                        Thumbnail.MakeThumbnailImage(PathHelper.Map(OFilePath), PathHelper.Map(s_FilePath), img_width, img_height);
                    }
                    if (img_iswater > 0)
                    {
                        var w_FilePath = strUtil.Join(sys.Path.Root, FilePath + FileName + "_W" + fileextname);
                        if (img_iswater == 1)
                        {
                            Thumbnail.TextWatermark(PathHelper.Map(OFilePath), configsService.GetValue("img_wmt_text"), "Verdana", cvt.ToInt(configsService.GetValue("img_wmt_size")), configsService.GetValue("img_wmt_color"), cvt.ToInt(configsService.GetValue("img_wmt_transparent")), cvt.ToInt(configsService.GetValue("img_wmt_pos")), PathHelper.Map(w_FilePath));
                            FileSystemObject.Delete(PathHelper.Map(OFilePath), FsoMethod.File);
                            FileSystemObject.Move(PathHelper.Map(w_FilePath), PathHelper.Map(OFilePath), FsoMethod.File);
                        }
                        else if (img_iswater == 2)
                        {
                            Thumbnail.ImageWatermark(PathHelper.Map(OFilePath), PathHelper.Map(strUtil.Join(sys.Path.Root, "Images/" + configsService.GetValue("img_wmi_file"))), cvt.ToInt(configsService.GetValue("img_wmi_transparent")), cvt.ToInt(configsService.GetValue("img_wmi_pos")), PathHelper.Map(w_FilePath));
                            FileSystemObject.Delete(PathHelper.Map(OFilePath), FsoMethod.File);
                            FileSystemObject.Move(PathHelper.Map(w_FilePath), PathHelper.Map(OFilePath), FsoMethod.File);
                        }
                    }
                    var iswidth = 1;
                    FileName modelFileName = new FileName();
                    modelFileName.username = ((User)ctx.viewer.obj).Name;
                    modelFileName.filename = FileName + fileextname;
                    modelFileName.filetype = filetype;
                    modelFileName.filepath = FilePath;
                    modelFileName.addtime = DateTime.Now;
                    modelFileName.iswidth = iswidth;
                    Result result = modelFileName.insert();
                    if (amid != 0)
                    {
                        Album_File modelAlbum_File = new Album_File();
                        modelAlbum_File.amid = amid;
                        modelAlbum_File.fiid = cvt.ToInt(db.RunScalar("select max(fiid) from ybb_album_file")) + 1;
                        modelAlbum_File.filetitle = FileName + fileextname;
                        modelAlbum_File.filename = FileName + fileextname;
                        modelAlbum_File.filepath = FilePath;
                        modelAlbum_File.iswidth = iswidth;
                        modelAlbum_File.addtime = DateTime.Now;
                        modelAlbum_File.insert();
                    }
                    ctx.web.ResponseWrite("true");
                    ctx.web.ResponseEnd();
                    break;
            }

        }

    }
}
